home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DICRC.C < prev    next >
C/C++ Source or Header  |  1993-06-15  |  3KB  |  119 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    dicrc.c
  5. //   Title:    Data File I/O Library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to the CRC for a logical data file.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <di.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //   Description:    Update the CRC of a data area
  49. //    Parameters:    pcsz            Physical file name. 
  50. //                        pcszLogical    Logical file to update
  51. //                        usType        Logical file type
  52. //       Returns:    TRUE if successful.
  53. //----------------------------------------------------------------------------
  54. BOOL FN_E DioCrc(PCSZ pcsz, PCSZ pcszLogical, USHORT usType, PCRC pcrc)
  55. {
  56.     HPF hpf;
  57.     HLF hlf = -1;
  58.     BOOL fUser;
  59.     BOOL fResult = FALSE;
  60.     HF hf;
  61.     SIZET cDir;
  62.     FPOS flen;
  63.     CRC crc;
  64.     DATADIR dir;
  65.  
  66.     if (pcrc)
  67.         *pcrc = 0;
  68.     if (HIUSHORT(pcsz))                        // Open physical file
  69.         {
  70.         if (!DioOpenPhysical(pcsz, &hpf, TRUE))
  71.             return FALSE;
  72.         fUser = FALSE;
  73.         }
  74.     else
  75.         {
  76.         hpf = LOUSHORT(pcsz);
  77.         Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);
  78.         Assert(di.physical[hpf].fUsed);
  79.         fUser = TRUE;
  80.         }
  81.                                                     // Not open logical file
  82.     if (!DioOpenLogical(pcszLogical, &hlf, usType))
  83.         goto ERROR_EXIT;
  84.     cDir = di.logical[hlf].cDir;
  85.     hf = di.logical[hlf].hf;
  86.     flen = di.logical[hlf].flen;
  87.     if ((flen % (FPOS)sizeof(DATAHDR)) != 0)
  88.         {
  89.         flen += sizeof(DATAHDR);            // Round up to nearest multiple of
  90.         flen /= sizeof(DATAHDR);            //  header size
  91.         flen *= sizeof(DATAHDR);
  92.         }
  93.                                                     // Calculate crc
  94.     if (!FileCrc(hf, &crc, flen, di.logical[hlf].fbase))
  95.         goto ERROR_EXIT;
  96.                                                     // Read directory
  97.     if (!DioDirRead(hpf, cDir, &dir))
  98.         goto ERROR_EXIT;
  99.  
  100.     dir.crcData = crc;
  101.     if (pcrc)
  102.         *pcrc = crc;
  103.                                                     // Write updated header
  104.     if (!DioDirWrite(hpf, cDir, &dir))
  105.         goto ERROR_EXIT;
  106.  
  107.     fResult = TRUE;
  108.  
  109. ERROR_EXIT:
  110.     if (hlf >= 0)
  111.         DioCloseLogical(hlf);
  112.     if (!fUser)
  113.         DioClosePhysical(hpf);
  114.     return fResult;
  115. }
  116. //----------------------------------------------------------------------------
  117. //------------------------------- End of File --------------------------------
  118. //----------------------------------------------------------------------------
  119.